package cn.sms.user.service.impl;

import cn.sms.basic.constant.UserConstants;
import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.service.impl.BaseServiceImpl;
import cn.sms.basic.util.MD5Utils;
import cn.sms.basic.util.StrUtils;
import cn.sms.user.domain.Logininfo;
import cn.sms.user.domain.User;
import cn.sms.user.dto.UserDto;
import cn.sms.user.mapper.LogininfoMapper;
import cn.sms.user.mapper.UserMapper;
import cn.sms.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void goToRegister(UserDto dto) {
        if(StringUtils.isEmpty(dto.getPhone())
                ||StringUtils.isEmpty(dto.getVerifyCode())
                ||StringUtils.isEmpty(dto.getPassword())
                ||StringUtils.isEmpty(dto.getConfirmPassword())){
            throw new BusinessException("空格不能为空");
        }
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        User user = userMapper.byPhoneQuery(dto.getPhone());
        if (user != null){
            throw new BusinessException("该用户已存在，请您直接登陆");
        }
        /*
         * key = "Verifycode:"+phone;
         * value = xxxxxx:时间戳
         */
        Object value = redisTemplate.opsForValue().get(UserConstants.PHONE_REGISTER+dto.getPhone());
        if(value ==null){
            throw new BusinessException("验证码已过期，请重新获取");
        }
        String[] arr = value.toString().split(":");
        if(!arr[0].equalsIgnoreCase(dto.getVerifyCode())){
            throw new BusinessException("请输入正确的验证码");
        }
        User user1 = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user1);
        logininfoMapper.save(logininfo);
        user1.setLogininfo_id(logininfo.getId());
        userMapper.save(user1);

    }

    private Logininfo user2Logininfo(User user1) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user1, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(UserDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(dto.getPassword()+salt);
        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }
}
